/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package Comunicator;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import multiplayer.PanelController;
import multiplayer.PhaseControllerCliente1;
import multiplayer.PhaseControllerOtrosClientes;
import multiplayer.RendererCliente1;
import multiplayer.RendererOtrosClientes;


public class PrimeraConexion {

    private PanelController panelController;
    
    public PrimeraConexion(PanelController panelController) {
        this.panelController = panelController;
    }

    public void connection() {
        try {
            Socket socket = new Socket("5.58.120.60", 5000);           
            
            InputStream iStream = socket.getInputStream();
            ObjectInputStream i = new ObjectInputStream(iStream);
            int numeroCliente = (Integer) i.readObject();
            System.out.println(numeroCliente);
            
            i.close();
            
            ServerSocket serverSocket = new ServerSocket(5001);
            socket = serverSocket.accept();
            InputStream iStream2 = socket.getInputStream();
            ObjectInputStream i2 = new ObjectInputStream(iStream2);
            String ip = (String) i2.readObject();
            System.out.println(ip);
            
            i2.close();
            
            if(numeroCliente == 1){
                PhaseControllerCliente1 c1PhaseController = new PhaseControllerCliente1();
                panelController.addKeyListener(c1PhaseController);
                panelController.getPanelPrincipal().addKeyListener(c1PhaseController);
                c1PhaseController.start();
                InputComunicatorCliente1 c1InputComunicator = new InputComunicatorCliente1(ip, c1PhaseController);
                c1InputComunicator.start();
                OutputComunicatorCliente1 c1OutputComunicator = new OutputComunicatorCliente1(ip, c1PhaseController.getScene());
                c1OutputComunicator.start();
                RendererCliente1 c1Renderer = new RendererCliente1(panelController.getPanelPrincipal(), c1PhaseController.getScene());
                c1Renderer.start();
            }
            else if(numeroCliente == -1){
                PhaseControllerOtrosClientes c2PhaseController = new PhaseControllerOtrosClientes();
                panelController.addKeyListener(c2PhaseController);
                panelController.getPanelPrincipal().addKeyListener(c2PhaseController);
                c2PhaseController.start();
                OutputComunicatorOtrosClientes c2OuputComunicator = new OutputComunicatorOtrosClientes(ip, c2PhaseController);
                c2OuputComunicator.start();
                RendererOtrosClientes c2Renderer = new RendererOtrosClientes(panelController.getPanelPrincipal());
                c2Renderer.start();
                InputComunicatorOtrosClientes c2InputComunicator = new InputComunicatorOtrosClientes(ip, c2Renderer);
                c2InputComunicator.start();
                
            }
           
        }catch (Exception ex) {
            Logger.getLogger(PrimeraConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
